﻿<?php
include_once(dirname(__FILE__).'/ProductFactory.class.php');
include_once(dirname(__FILE__).'/PriceCache.class.php');

class Product{
    private $id; // 产品id
    /**
     * 材料列表，包含每种材料的价格
     * $materials['id'] 材料id
     * $materials['name'] 材料名称
     * $materials['quantity'] 材料需求量
     * $materials['damage_per_job'] 材料损耗系数
     * $materials['sell'] 市场最低卖价 
     * $materials['buy']  市场最高买价 
     * @var array 
     */
    private $materials;
    private $price_cache;

    function __construct($id,$materials){
        $this->id = $id;
        $this->materials = $materials;
        $this->price_cache = PriceCache::getInstance();
        $this->getPriceList();
    }
    public function toJson(){
        return json_encode($this->materials);
    }
    private function getPriceList(){
        $price_list = array();
        foreach($this->materials as &$material)
        {
            $price = $this->getPrice($material['id']);
            $material['sell'] = $price['sell'];
            $material['buy'] = $price['buy'];
        }
    }
    private function getPrice($id){
        return $this->price_cache->get($id);
    }
}
?>
